#!/bin/sh

# ulimit -n 1024000
# ulimit -c unlimited

p='collect'

# 编译包
# export GODEBUG=madvdontneed=1
export GOPATH='/root/aist/goProject'
export GO111MODULE='on'
export GOPROXY='https://goproxy.cn'
go build -o $p

function killServer() {
    pid=$(ps x | grep -w "$p" | sed -e '/mykill/d' | sed -e '/grep/d' | sed -e '/tail/d' | awk '{print $1}')
    pid=$(echo $pid | awk '{print $1}')
    while [ ! -z "$pid" ]; do
        kill -9 $pid
        pid=$(ps x | grep -w "$p" | sed -e '/grep/d' | sed -e '/tail/d' | awk '{print $1}')
        pid=$(echo $pid | awk '{print $1}')
    done
}

case $1 in
start)
    killServer
    sleep 1
    nohup ./$p >>./out.log 2>&1 &
    sleep 1
    echo ""
    ps -elf | grep $p
    ;;
stop)
    killServer
    sleep 1
    echo ""
    ps -elf | grep $p
    ;;
restart)
    killServer
    sleep 1
    nohup ./$p >>./out.log 2>&1 &
    sleep 1
    echo ""
    ps -elf | grep $p
    ;;
*)
    killServer
    sleep 1
    nohup ./$p >>./out.log 2>&1 &
    sleep 1
    echo ""
    ps -elf | grep $p
    ;;
esac
